package com.example.administrator.solution;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.menu.MenuBuilder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.solution.util.AsyncTaskUpdateView;
import com.obsez.android.lib.filechooser.ChooserDialog;
import java.io.File;


public class MainActivity extends AppCompatActivity {

    private TextView vwEdtFileName;
    private TextView vwTxtPercentage;
    private ProgressBar vwProgressBar;
    private TextView vwTvLogsText;
    private int pro;
    private String _path;
    private File _file;

    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vwEdtFileName = findViewById(R.id.vw_edt_file_name);
        vwEdtFileName.setFocusable(false);
        vwEdtFileName.setFocusableInTouchMode(false);
        ImageView vwChooseFile = findViewById(R.id.vw_choose_file);
        Button vwSendFile = findViewById(R.id.vw_send_file);
        vwTxtPercentage = findViewById(R.id.vw_txt_percentage);
        vwProgressBar = findViewById(R.id.vw_progress_bar);
        vwTvLogsText = findViewById(R.id.vw_tvLogs_text);

        fileChooserDialog();

        vwChooseFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fileChooserDialog();
            }
        });
        vwSendFile.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("StaticFieldLeak")
            @Override
            public void onClick(View v) {
                if(_file!=null){
                    new AsyncTaskUpdateView(MainActivity.this,vwProgressBar,vwTxtPercentage).execute();
                }
            }
        });

    }

    private void fileChooserDialog() {

        new ChooserDialog().with(MainActivity.this)
                .withStartFile(_path)
                .withChosenListener(new ChooserDialog.Result() {
                    @Override
                    public void onChoosePath(String path, File pathFile) {
                        vwEdtFileName.setText(pathFile.getName());
                        _path = path;
                        _file = pathFile;
                    }
                })
                .withOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        dialog.dismiss();
                    }
                })
                .build()
                .show();
    }

    //  menu icon
    @SuppressLint("RestrictedApi")
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_gotosetting, menu);

        if(menu instanceof MenuBuilder){
            MenuBuilder m = (MenuBuilder) menu;
            //noinspection RestrictedApi
            m.setOptionalIconsVisible(true);
        }

        return super.onCreateOptionsMenu(menu);
    }
    // Option Menu Click Listener
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.setting:
                Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
                startActivity(intent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
